package com.tianyi.sfzy1.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyi.sfzy1.util.R;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录认证
        //获取请求头中的token
        String token = request.getHeader("token");


        //判断是否有token
        if(token!=null){
            return true;
        }else{
            R r = R.error2("请登录后再操作");

            ServletOutputStream out = response.getOutputStream();

            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(r);
            out.write(json.getBytes());
            out.close();

            return false;
        }

    }
}